package com.web.servlet.remark;

import com.web.bean.remark.Answer;
import com.web.bean.remark.Remark;
import com.web.result.Result;
import com.web.service.remark.RemarkService;
import com.web.utils.JSONWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @autor Chen
 * @create 2021--25-14:18
 * @desc
 */
@WebServlet("/remark/*")
public class RemarkServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");
        String uri=req.getRequestURI().replace("/remark/","");
        /*uri=addRemarkForNovel,通过传入Remark的bean添加小说总评*/
        if ("addRemarkForNovel".equals(uri)){
//            System.out.println("a");
            addRemarkForNovel(req,resp);
        }
        /*selectRemarkForNovelById，根据小说名字查找该小说的总评*/
        else if ("selectRemarkForNovelById".equals(uri)){
//            System.out.println("b");
            selectRemarkForNovelById(req,resp);
        }
        /*selectRemarkForChapter,根据rid和chapterid查找评论*/
        else if ("selectRemarkForChapter".equals(uri)){
            System.out.println("c");
            selectRemarkForChapter(req,resp);
        }
        /*uri=addRemarkForChapter,通过传入Remark的bean添加章节评论*/
        else if ("addRemarkForChapter".equals(uri)){
//            System.out.println("d");
            addRemarkForChapter(req,resp);
        }
        /*deleteRemarkForNovel,通过id删除小说总评*/
        else if ("deleteRemarkForNovel".equals(uri)){
            deleteRemarkForNovel(req,resp);
        }
        /*deleteRemarkForChapter,通过id删除章节评论*/
        else if ("deleteRemarkForChapter".equals(uri)){
            deleteRemarkForChapter(req,resp);
        }
        /*answerForNovel,对小说总评下的评论进行回复*/
        else if ("answerForNovel".equals(uri)){
            answerForNovel(req,resp);
        }
        /*answerForChapter,对小说章节下的评论进行回复*/
        else  if ("answerForChapter".equals(uri)){
            answerForChapter(req,resp);
        }
        /*跳转到写评论页面*/
        else if("toWriteRemarkPage".equals(uri)){
            String rid=getInitParameter("rid");
            System.out.println("rid"+rid);
            toWriteRemarkPage(req,resp);
        }else if ("addReply".equals(uri)){
            addReply(req,resp);
        }
        else if ("toReply".equals(uri)){
            toReply(req,resp);
        }else if("deleSession".equals(uri)){
            HttpSession session=req.getSession();
            session.invalidate();
            req.getRequestDispatcher("/novelRemark.jsp").forward(req,resp);
        }else if ("toselectAnswer".equals(uri)){
            req.setCharacterEncoding("utf-8");
            HttpSession session=req.getSession();
            String rid=req.getParameter("rid");
            session.setAttribute("rid",rid);
            req.getRequestDispatcher("/answers.jsp").forward(req,resp);
        }
        else if ("selectAnswer".equals(uri)){
            selectAnswer(req,resp);
        }
    }

    private void selectAnswer(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException{
        req.setCharacterEncoding("utf-8");
        String rid=req.getParameter("rid");
        Result result=new Result();
        RemarkService remarkService=new RemarkService();
        List<Answer> answers=new ArrayList<>();
//        Answer answer=new Answer();
        try {
            answers=remarkService.selectAnswerService(rid);
            result.setFlag(true);
            result.setMessage("成功");
            result.setData(answers);
        }catch (Exception e){
            result.setFlag(false);
            result.setMessage("失败");
        }finally {
            JSONWriter.writeJSON(result,resp);
        }
    }

    private void toReply(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException{
        req.setCharacterEncoding("utf-8");
        String rid=req.getParameter("rid");
        HttpSession session=req.getSession();
        session.setAttribute("rid",rid);
        req.getRequestDispatcher("/WriteReply.jsp").forward(req,resp);
    }

    private void addReply(HttpServletRequest req, HttpServletResponse resp) {
        Result result=new Result();
        try {
            req.setCharacterEncoding("utf-8");
            String rid=req.getParameter("rid");
            Date createTime=new Date();
            RemarkService remarkService=new RemarkService();
            String aid=remarkService.searchFreeAid().toString();
            String replyContent=req.getParameter("replyContent");
            Answer answer=new Answer(aid,rid,createTime,replyContent);
            remarkService.addReply(answer);
            result.setFlag(true);
            result.setMessage("回复成功");
        }catch (Exception exception){
            result.setFlag(false);
            result.setMessage(exception.getMessage());
        }finally {
            try {
                req.getRequestDispatcher("/novelRemark.jsp").forward(req,resp);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    private void toWriteRemarkPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException {
        req.setCharacterEncoding("utf-8");
        String nid=req.getParameter("nid");
        String uid=req.getParameter("uid");
        String chapterid_1=req.getParameter("chapterid");
        String chapterid=null;
        String replyCount=req.getParameter("replyCount");
//        System.out.println("chapterid_1:"+chapterid_1.length());
        if (chapterid_1.length()==0){
            chapterid=null;
        }else {
            chapterid=chapterid_1;
        }
        String type=req.getParameter("type");
        HttpSession session=req.getSession();
        session.setAttribute("nid",nid);
        session.setAttribute("uid",uid);
        session.setAttribute("chapterid",chapterid);
        session.setAttribute("type",type);
        session.setAttribute("replyCount",replyCount);
        req.getRequestDispatcher("/WriterRemark.jsp").forward(req,resp);
    }

    private void selectRemarkForChapter(HttpServletRequest req, HttpServletResponse resp) {
        Result result=new Result();
        RemarkService remarkService=new RemarkService();
        List<Remark> remarks=new ArrayList<>();
        try {
            remarks=remarkService.selectRemarkForChapter(req.getParameter("nid"),req.getParameter("chapterid"));
            result.setFlag(true);
            result.setMessage("查询成功");
            result.setData(remarks);
            result.setData(remarks);
        }catch (Exception exception){
            result.setFlag(false);
            result.setMessage(exception.getMessage());
        }finally {
            JSONWriter.writeJSON(result,resp);
        }
    }

    private void selectRemarkForNovelById (HttpServletRequest req, HttpServletResponse resp) {
        Result result=new Result();
        String nid=req.getParameter("nid");
        RemarkService remarkService=new RemarkService();
        List<Remark> remarks=new ArrayList<>();
        try {
            remarks=remarkService.searchRemarkForNoveById(nid);
            result.setFlag(true);
            result.setMessage("查询成功");
            result.setData(remarks);
        }catch (Exception exception){
            result.setFlag(false);
            result.setMessage(exception.getMessage());
        }finally {
            JSONWriter.writeJSON(result,resp);
        }
    }

    private void answerForChapter(HttpServletRequest req, HttpServletResponse resp) {

    }

    private void answerForNovel(HttpServletRequest req, HttpServletResponse resp) {
    }

    private void deleteRemarkForChapter(HttpServletRequest req, HttpServletResponse resp) {

    }

    private void deleteRemarkForNovel(HttpServletRequest req, HttpServletResponse resp) {
        Result result=new Result();
        RemarkService remarkService=new RemarkService();
        try {
            remarkService.deleteRemarkForNoveByRid(req.getParameter("rid"));
            System.out.println(req.getParameter("rid"));
            result.setFlag(true);
            result.setMessage("删除成功");
        }catch (Exception exception){
            result.setFlag(false);
            result.setMessage(exception.getMessage());
        }
    }

    private void addRemarkForChapter(HttpServletRequest req, HttpServletResponse resp) {
        RemarkService remarkService=new RemarkService();
        Result result=new Result();
        Remark remark=new Remark();
        try {
            req.setCharacterEncoding("utf-8");
            /*调用寻找可用id的方法*/
            String rid=remarkService.searchFreeId().toString();
            remark.setRid(rid);
            remark.setNid(req.getParameter("nid"));
            remark.setUid(req.getParameter("uid"));
            remark.setRmtime(new Date());
            System.out.println(req.getParameter("content"));
            System.out.println(req.getParameter("nid"));
            remark.setContent(req.getParameter("content"));
            remark.setChapterid( req.getParameter("chapterid"));
            remark.setType(req.getParameter("type"));
            remarkService.addToNovelService(remark);
            result.setFlag(true);
            result.setMessage("新增成功");
        }catch (Exception exception){
            result.setFlag(false);
            result.setMessage(exception.getMessage());
        }finally {
            try {
                req.getRequestDispatcher("/chapterRemark.jsp").forward(req,resp);
            }catch (Exception e){
                result.setMessage(e.getMessage());
            }
        }
    }

    private void addRemarkForNovel(HttpServletRequest req, HttpServletResponse resp) {
        RemarkService remarkService=new RemarkService();
        Result result=new Result();
        Remark remark=new Remark();
        try {
            req.setCharacterEncoding("utf-8");
            /*调用寻找可用id的方法*/
            String rid=remarkService.searchFreeId().toString();
            remark.setRid(rid);
            remark.setNid(req.getParameter("nid"));
            remark.setUid(req.getParameter("uid"));
            remark.setRmtime(new Date());
            System.out.println(req.getParameter("content"));
            System.out.println(req.getParameter("nid"));
            remark.setContent(req.getParameter("content"));
            remark.setChapterid( req.getParameter("chapterid"));
            remark.setType(req.getParameter("type"));
            remark.setReplyCount(req.getParameter("replyCount"));
            System.out.println("replyC:"+req.getParameter("replyCount"));
            remarkService.addToNovelService(remark);
            result.setFlag(true);
            result.setMessage("新增成功");
        }catch (Exception exception){
            result.setFlag(false);
            result.setMessage(exception.getMessage());
        }finally {
            try {
                req.getRequestDispatcher("/novelRemark.jsp").forward(req,resp);
            }catch (Exception e){
                result.setMessage(e.getMessage());
            }
        }
    }
}
